package com.example.coursehub.coursemanagement.service.impl;

import com.example.coursehub.coursemanagement.entity.Course;
import com.example.coursehub.coursemanagement.entity.Chapter;
import com.example.coursehub.coursemanagement.entity.Material;
import com.example.coursehub.coursemanagement.repository.CourseRepository;
import com.example.coursehub.coursemanagement.repository.ChapterRepository;
import com.example.coursehub.coursemanagement.repository.MaterialRepository;
import com.example.coursehub.coursemanagement.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 课程管理服务实现类
 */
@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    
    @Autowired
    private CourseRepository courseRepository;
    
    @Autowired
    private ChapterRepository chapterRepository;
    
    @Autowired
    private MaterialRepository materialRepository;
    
    // 课程相关操作
    @Override
    public Course createCourse(Course course) {
        return courseRepository.save(course);
    }
    
    @Override
    public Course updateCourse(Long id, Course course) {
        Optional<Course> existingCourse = courseRepository.findById(id);
        if (existingCourse.isPresent()) {
            Course updatedCourse = existingCourse.get();
            updatedCourse.setName(course.getName());
            updatedCourse.setDescription(course.getDescription());
            updatedCourse.setTeacherId(course.getTeacherId());
            return courseRepository.save(updatedCourse);
        }
        throw new RuntimeException("课程不存在，ID: " + id);
    }
    
    @Override
    public void deleteCourse(Long id) {
        if (courseRepository.existsById(id)) {
            // 删除相关的章节和资料
            chapterRepository.deleteByCourseId(id);
            materialRepository.deleteByCourseId(id);
            courseRepository.deleteById(id);
        } else {
            throw new RuntimeException("课程不存在，ID: " + id);
        }
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<Course> getCourseById(Long id) {
        return courseRepository.findById(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<Course> getAllCourses() {
        return courseRepository.findAll();
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<Course> getCoursesByTeacher(Long teacherId) {
        return courseRepository.findByTeacherId(teacherId);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<Course> searchCoursesByName(String name) {
        return courseRepository.findByNameContaining(name);
    }
    
    // 章节相关操作
    @Override
    public Chapter createChapter(Chapter chapter) {
        return chapterRepository.save(chapter);
    }
    
    @Override
    public Chapter updateChapter(Long id, Chapter chapter) {
        Optional<Chapter> existingChapter = chapterRepository.findById(id);
        if (existingChapter.isPresent()) {
            Chapter updatedChapter = existingChapter.get();
            updatedChapter.setTitle(chapter.getTitle());
            updatedChapter.setContent(chapter.getContent());
            updatedChapter.setOrderNum(chapter.getOrderNum());
            return chapterRepository.save(updatedChapter);
        }
        throw new RuntimeException("章节不存在，ID: " + id);
    }
    
    @Override
    public void deleteChapter(Long id) {
        if (chapterRepository.existsById(id)) {
            // 删除相关的资料
            materialRepository.deleteByChapterId(id);
            chapterRepository.deleteById(id);
        } else {
            throw new RuntimeException("章节不存在，ID: " + id);
        }
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<Chapter> getChapterById(Long id) {
        return chapterRepository.findById(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<Chapter> getChaptersByCourse(Long courseId) {
        return chapterRepository.findByCourseIdOrderByOrderNum(courseId);
    }
    
    // 资料相关操作
    @Override
    public Material createMaterial(Material material) {
        return materialRepository.save(material);
    }
    
    @Override
    public Material updateMaterial(Long id, Material material) {
        Optional<Material> existingMaterial = materialRepository.findById(id);
        if (existingMaterial.isPresent()) {
            Material updatedMaterial = existingMaterial.get();
            updatedMaterial.setName(material.getName());
            updatedMaterial.setFilePath(material.getFilePath());
            updatedMaterial.setFileType(material.getFileType());
            updatedMaterial.setFileSize(material.getFileSize());
            return materialRepository.save(updatedMaterial);
        }
        throw new RuntimeException("资料不存在，ID: " + id);
    }
    
    @Override
    public void deleteMaterial(Long id) {
        if (materialRepository.existsById(id)) {
            materialRepository.deleteById(id);
        } else {
            throw new RuntimeException("资料不存在，ID: " + id);
        }
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<Material> getMaterialById(Long id) {
        return materialRepository.findById(id);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<Material> getMaterialsByCourse(Long courseId) {
        return materialRepository.findByCourseId(courseId);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<Material> getMaterialsByChapter(Long chapterId) {
        return materialRepository.findByChapterId(chapterId);
    }
}